package PaperExperiment;

import java.util.*;

import XmlLoader.FGObjProcess;
import XmlLoader.FGObjectTrajectoryStructure;

public class LCSSdistance {

	private double delta = 0;
	public void setdelta(double x)
	{
		this.delta = x;
	}
	
	public int LCSS_2D(ArrayList<Integer[]> A, ArrayList<Integer[]> B)  //for arraylist
	{
		int[][] sol = new int[A.size()+1][B.size()+1];
		for (int i = 0; i < A.size()+1; i++)
			for(int j = 0; j< B.size()+1; j++)
				
				if(i == 0 || j == 0)
					sol[i][j] = 0;
				else if ((A.get(i-1)[0]-B.get(j-1)[0])*(A.get(i-1)[0]-B.get(j-1)[0]) + (A.get(i-1)[1]-B.get(j-1)[1])*(A.get(i-1)[1]-B.get(j-1)[1]) < this.delta*this.delta)
					sol[i][j] = 1 + sol[i-1][j-1];
				else
					sol[i][j] = sol[i-1][j] > sol[i][j-1]?sol[i-1][j]:sol[i][j-1]; 
		return sol[A.size()][B.size()];
	}
	
	

	
	public static void main(String[] args) {
		long startTimeBuild = System.currentTimeMillis();
		FGObjProcess fgObj = new FGObjProcess(args[1]);
		//training data
		List<String> list_objIndex = fgObj.ACT004_getStringList(args[0]);
    	List<FGObjectTrajectoryStructure> list = fgObj.ACT001_getListOfObj(list_objIndex);
    	//testing data
    	List<String> list_objIndex2 = fgObj.ACT004_getStringList(args[2]);
    	List<FGObjectTrajectoryStructure> listTest = fgObj.ACT001_getListOfObj(list_objIndex2);
    	
    	LCSSdistance lcs = new LCSSdistance();
    	lcs.setdelta(20); //distances between point is smaller than 10 are common point 
    	
    	double threshold_abnormal = 8.0; //at least the number of common point
    	ArrayList<Integer> abnormal_traj_id = new ArrayList<Integer>();
    	
    	System.out.println("Database Load time " + (System.currentTimeMillis() - startTimeBuild));
    	long startTimeTest = System.currentTimeMillis();
    	
    	//all lcss sum between trajectories
    	for (int i = 0; i<listTest.size();i++)
    	{
    		int total_LCSS = 0;
    		int size = listTest.get(i).GET002_nNumberOfPoint();
    		ArrayList<Integer[]> X = new ArrayList<Integer[]>();
    		
    		for (int j = 0; j < size; j++) //trajectory point
    		{
    			Integer[] a = {listTest.get(i).GET003_elementIn_list_fGObject(j).GET001_centerReal().x, listTest.get(i).GET003_elementIn_list_fGObject(j).GET001_centerReal().y}; 
    			X.add(a);
    		}
    		//System.out.println("i = "+i);
    		for (int j = 0; j<list.size(); j++) //another trajectory Y
    		{
    			if(i != j)
    			{
    				ArrayList<Integer[]> Y = new ArrayList<Integer[]>();
    				int size2 = list.get(j).GET002_nNumberOfPoint();
    				for (int k = 0; k < size2; k++)
    				{
    					Integer[] a = {list.get(j).GET003_elementIn_list_fGObject(k).GET001_centerReal().x, list.get(j).GET003_elementIn_list_fGObject(k).GET001_centerReal().y}; 
    					Y.add(a);
    				}
    				total_LCSS += lcs.LCSS_2D(X, Y);
    				
    			}
    		}
    		//System.out.println("id = " + listTest.get(i).GET001_nobjNewNumIndex() + ":total_LCSS = " + total_LCSS );
    		if(total_LCSS <= threshold_abnormal*list.size())
    			System.out.println(true);
    		else
    			System.out.println(false);
    	}
    	System.out.println("Testing time " + (System.currentTimeMillis() - startTimeTest));
	}
}
